package com.ushio.gougouwangguo.config.redis;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RedissonConfig {
    @Value("${spring.data.redis.host}")
    private String host;
    @Value("${spring.data.redis.port}")
    private String prot;

    @Bean(destroyMethod = "shutdown") // Spring 容器关闭时自动关闭 Redisson
    public RedissonClient redissonClient() {
        Config config = new Config();

        // 单节点 Redis 配置
        config.useSingleServer()
                .setAddress("redis://"+host+":"+prot)  // Redis 地址
                //.setPassword("yourPassword")          // 如果有密码可以加上
                .setDatabase(0);                        // Redis 库

        // 使用 JSON 序列化，解决二进制乱码问题
        config.setCodec(new JsonJacksonCodec());

        return Redisson.create(config);
    }
}
